﻿Imports System.IO
Imports Magic.Core

Public Class HaloMap
#Region "Constants"
    Public Enum MapFormat As Integer
        Xbox = 5
        PC = 7
        CE = 609
        Undefined = -1
    End Enum
    Public Enum MapUsage As Integer
        Campaign = 0
        Multiplayer = 1
        UI = 2
    End Enum
    Public Enum HeaderLengths As Short
        Xbox = 36
        PC = 40
        CE = 40
    End Enum
    Public Enum BspMagic As Integer
        Xbox = &H819A6000
    End Enum
    Public Enum IndexMagic As Integer
        Xbox = &H803A6024
        CE = &H40440028
    End Enum
    Public Class MapBuildVersion
        Public Const NTSCXbox As String = "01.10.12.2276"
        Public Const PALXbox As String = "01.01.14.2342"
        Public Const PC As String = "01.00.00.0564"
        Public Const CE As String = "01.00.00.0609"
    End Class
#End Region
    Private tags As TagIndex
    Private _fourcc As String
    Private _format As MapFormat
    Private _usage As MapUsage
    Private _build As MapBuildVersion
    Sub New(filename As String)
        tags = New TagIndex()

        'sLabel = status;
        'structs = structureList;
        'filename = file
        'tagDirectory = tagDirectoryName;
        'Status("Preparing to load file...");
        Dim fileExtension As String = IO.Path.GetExtension(filename).Remove(0, 1)
        If (fileExtension = "map") Then
            LoadMap(filename)
        ElseIf (fileExtension = "scenario") Then
            LoadScenario(filename)
        Else
            Throw New Exception("Invalid file to load as a map object.")
        End If
        GC.Collect()
    End Sub
    Private Sub LoadScenario(Filename As String)

        Dim scenarioTag As HaloTag = tags.GetDependencyTag(Filename)
        scenarioTag.ToTagFile(Filename & ".test")
        Dim globalsTag As HaloTag = Nothing

        Dim MapUsage As HaloMap.MapUsage = CType(scenarioTag, SCENARIO_TAG).type
        Dim process As String = ""
        Select Case MapUsage

            Case 0
                process = "ui\shell\solo"
                globalsTag = tags.GetDependencyTag("globals\\campaign", "matg")', tagDirectory, "globals\\globals", False, ceTagsDirectory)];

            Case 1
                process = "ui\shell\multiplayer"
                globalsTag = tags.GetDependencyTag("globals\\multiplayer", "matg")', tagDirectory, "globals\\globals", False, ceTagsDirectory)];

            Case 2

                process = "ui\shell\main_menu"
                globalsTag = tags.GetDependencyTag("globals\\main_menu", "matg") ', tagDirectory, "globals\\globals", False, ceTagsDirectory)];

        End Select

        Dim tagMultiplayerText As HaloTag = tags.GetDependencyTag("ui\\multiplayer_game_text", "ustr") ', tagDirectory, ceTagsDirectory)];
        Dim tagDefaultWhite As HaloTag = tags.GetDependencyTag("ui\shell\bitmaps\white", "bitm") ', tagDirectory, ceTagsDirectory)];
        Dim tagUserInterface As HaloTag = tags.GetDependencyTag(process, "Soul") ', tagDirectory, ceTagsDirectory)];
    End Sub
    Private Sub LoadMap(Filename As String)
        Dim er As New EndianReader(New FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), ByteOrder.LittleEndian)
        'daeh
        _fourcc = er.ReadFourCC()
        'Map format
        _format = er.ReadInt32()
        Select Case _format
            Case MapFormat.CE, MapFormat.PC
                'TODO: Fix aux streams
                'bitmaps = New EndianReader(New FileStream(ceBitmaps, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), ByteOrder.LittleEndian)
                'sounds = New EndianReader(New FileStream(ceSounds, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), ByteOrder.LittleEndian)

            Case MapFormat.Xbox
                'bitmaps = null
                'sounds = null
            Case Else
                Throw New Exception("Invalid map format.")
        End Select
        'Total Decompressed size
        Dim _size As Integer = er.ReadInt32
        If _size > er.Length Then
            'TODO: Decompress Stream
        End If

        Dim _indexOffset As Integer = er.ReadInt32



    End Sub
End Class
